﻿#include "calendarfragment.h"
#include "event.h"
#include "loader.h"
#include "ui_calendarfragment.h"
#include "dayincalendarfragment.h"
#include <QLabel>
#include <QMap>
#include <QDir>
#include <QDebug>
#include <QCalendar>
#include <QMessageBox>
CalendarFragment::CalendarFragment(int year,int month,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CalendarFragment) {

    ui->setupUi(this);
    this->year = year;
    this->month = month;
    loadUI();
}

CalendarFragment::~CalendarFragment() {
    delete ui;
}

void CalendarFragment::on_pushButton_nextMonth_clicked() {
    clearAll();
    if(this->month==12)year++,month = 1;
    else month++;
    loadUI();
}

void CalendarFragment::loadUI() {
    ui->label_date->setText(QString(tr("%1 year %2 month")).arg(year).arg(month));
    QFont font;
    font.setPointSize(30);
    QLabel *mon = new QLabel();
    mon->setText("Mon");
    mon->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    mon->setFont(font);
    ui->gridLayout->addWidget(mon,0,0);
    QLabel *tue = new QLabel();
    tue->setText("Tue");
    tue->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    tue->setFont(font);
    ui->gridLayout->addWidget(tue,0,1);
    QLabel *wed = new QLabel();
    wed->setText("Wed");
    wed->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    wed->setFont(font);
    ui->gridLayout->addWidget(wed,0,2);
    QLabel *thu = new QLabel();
    thu->setText("Thu");
    thu->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    thu->setFont(font);
    ui->gridLayout->addWidget(thu,0,3);
    QLabel *fri = new QLabel();
    fri->setText("Fri");
    fri->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    fri->setFont(font);
    ui->gridLayout->addWidget(fri,0,4);
    QLabel *sat = new QLabel();
    sat->setText("Sat");
    sat->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    sat->setFont(font);
    ui->gridLayout->addWidget(sat,0,5);
    QLabel *sun = new QLabel();
    sun->setText("Sun");
    sun->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    sun->setFont(font);
    ui->gridLayout->addWidget(sun,0,6);
    QCalendar calendar;

    int startDayInWeek = calendar.dayOfWeek(QDate(year,month,1));
    int lastY = year,lastMon = month;
    if(lastMon==1)lastMon=12,lastY--;
    else lastMon--;
    int lastMonthDays = calendar.daysInMonth(lastMon,lastY);
    if(startDayInWeek!=1) {
        for(int i = 1; i<startDayInWeek; i++) {
            QVector<Event> events = Loader::loadEvents(QString("./savings/calendar_event_%1_%2_%3.json").arg(lastY).arg(lastMon).arg(lastMonthDays-i));
            QMap<QString,Event> eves;
            for(auto &x:events)eves[x.getName()] = x;
            ui->gridLayout->addWidget(new DayInCalendarFragment(eves,lastY,lastMon,lastMonthDays-i),1,startDayInWeek-i-1);
        }
    }

    int day = 1;
    int col = 0,row = 0;
    for(int i = 0; i<7; i++) {
        int j = i==0?startDayInWeek-1:0;
        col = i;
        for(; j<7; j++) {

            QVector<Event> events = Loader::loadEvents(QString("./savings/calendar_event_%1_%2_%3.json").arg(year).arg(month).arg(day));
            QMap<QString,Event> eves;
            for(auto &x:events)eves[x.getName()] = x;
            ui->gridLayout->addWidget(new DayInCalendarFragment(eves,year,month,day),i+1,calendar.dayOfWeek(QDate(year,month,day))-1);
            day++;
            if(day>calendar.daysInMonth(month,year)) {
                if(j==6)col--;
                row = j;
                break;
            }
        }
        if(day>calendar.daysInMonth(month,year))break;
    }

    col++;
    int nextY,nextMon;
    if(month==12)nextY=year+1,nextMon = 1;
    else nextY = year,nextMon = month+1;
    int lastDayofWeek = calendar.dayOfWeek(QDate(nextY,nextMon,1));
    day = 1;

    for(int i = row+1; i<7; i++) {
        QVector<Event> events = Loader::loadEvents(QString("./savings/calendar_event_%1_%2_%3.json").arg(nextY).arg(nextMon).arg(day));
        QMap<QString,Event> eves;
        for(auto &x:events)eves[x.getName()] = x;
        ui->gridLayout->addWidget(new DayInCalendarFragment(eves,nextY,nextMon,day),col,i);
        day++;
    }
}

void CalendarFragment::clearAll() {
    QLayoutItem *child;
    while ((child = ui->gridLayout->takeAt(0)) != 0) {
        //setParent为NULL，防止删除之后界面不消失
        if(child->widget()) {
            child->widget()->setParent(NULL);
        }

        delete child;
    }
}

void CalendarFragment::on_pushButton_previousMonth_clicked() {
    clearAll();
    if(this->month==1)year--,month = 12;
    else month--;
    loadUI();
}

void CalendarFragment::on_pushButton_clicked() {
    if(ui->lineEdit->text().isEmpty()) {
        QMessageBox::warning(this,tr("error"),tr("please input the name"));
    } else {
        QStringList totFiles,neededFiles;
        QDir dir("./savings/");
        totFiles = dir.entryList();
        for(auto &x:totFiles)if(x.contains("calendar_event"))neededFiles<<x;
        QString info;
        for(auto &name:neededFiles) {
            QVector<Event> vs = Loader::loadEvents("./savings/"+name);
            for(auto &entry:vs) {
                if(entry.getName().contains(ui->lineEdit->text()))info+=entry.getName()+tr("    time:")+entry.getTime().toString("yyyy:mm:dd hh:mm");
            }
        }
        if(info.isEmpty())QMessageBox::information(this,tr("Ouch"),tr("Noting Founded"));
        else QMessageBox::information(this,tr("Founded"),info);
    }
}
